package com.csun.cmny.provider.controller.admin;

import com.csun.cmny.core.support.BaseController;
import com.csun.cmny.provider.device.csun.camera.entity.CsunCameraData;
import com.csun.cmny.provider.model.constant.DeviceConstant;
import com.csun.cmny.provider.model.domain.DmcDevice;
import com.csun.cmny.provider.model.dto.version.VersionSetApplyDto;
import com.csun.cmny.provider.model.dto.version.VersionSetDto;
import com.csun.cmny.provider.model.dto.version.VersionSetPersonApply;
import com.csun.cmny.provider.model.dto.version.VersionSetQueryDto;
import com.csun.cmny.provider.model.enums.UacRoleCodeEnum;
import com.csun.cmny.provider.model.vo.ReviseVo;
import com.csun.cmny.provider.model.vo.RpcRoleVo;
import com.csun.cmny.provider.model.vo.csunversion.AppVersionDeviceVo;
import com.csun.cmny.provider.model.vo.csunversion.VersionDeviceVo;
import com.csun.cmny.provider.model.vo.csunversion.VersionSetVo;
import com.csun.cmny.provider.resolver.CmnyMessageSource;
import com.csun.cmny.provider.service.DmcCsunVersionSetService;
import com.csun.cmny.provider.service.UacRpcService;
import com.csun.cmny.util.PublicUtil;
import com.csun.cmny.util.wrapper.WrapMapper;
import com.csun.cmny.util.wrapper.Wrapper;
import com.github.pagehelper.PageInfo;
import com.google.common.collect.Lists;
import lombok.extern.slf4j.Slf4j;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

/**
 * @Auther: chisj chisj@foxmal.com
 * @Date: 2020/4/2 11:17
 * @Description: the class dmc csun version set controller
 */
@Slf4j
@RestController
@RequestMapping(value = "/versionSet", produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
public class DmcCsunVersionSetController extends BaseController {

    @Resource
    private CmnyMessageSource cmnyMessageSource;
    @Resource
    private DmcCsunVersionSetService dmcCsunVersionSetService;
    @Resource
    private UacRpcService uacRpcService;

    /**
     * 查询配置列表 - 分页
     *
     * @param versionSetQueryDto    the version set query dto
     *
     * @return  the wrapper.
     */
    @RequestMapping(value = "/queryListWithPage", method = RequestMethod.POST)
    public Wrapper queryListWithPage(@RequestBody VersionSetQueryDto versionSetQueryDto) {

        List<VersionSetVo> versionSetVoList = dmcCsunVersionSetService.queryListWithPage(versionSetQueryDto);

        return WrapMapper.wrap(Wrapper.SUCCESS_CODE,
                cmnyMessageSource.getMessage("operation.success", null),
                new PageInfo<>(versionSetVoList));
    }

    /**
     * 发布版本升级公告
     *
     * @param versionSetDto the version set dto
     *
     * @return  the wrapper.
     */
    @RequestMapping(value = "/add", method = RequestMethod.POST)
    public Wrapper add(@RequestBody VersionSetDto versionSetDto) {

        // TODO 运维才能发布版本
//        List<RpcRoleVo> roleVoList = uacRpcService.queryRoleListByUserId(getLoginAuthDto().getUserId());
//        if (!roleVoList.contains(UacRoleCodeEnum.ADMIN.getCode())) {
//            return WrapMapper.wrap(Wrapper.SUCCESS_CODE,
//                    cmnyMessageSource.getMessage("user.role.not.admin", null),
//                    null);
//        }

        Integer result = dmcCsunVersionSetService.add(versionSetDto, getLoginAuthDto());

        if (result < 1) {
            return WrapMapper.wrap(Wrapper.ERROR_CODE, cmnyMessageSource
                    .getMessage("operation.failed", null));
        }

        return WrapMapper.wrap(Wrapper.SUCCESS_CODE,
                cmnyMessageSource.getMessage("operation.success", null),
                result);
    }

    /**
     * query csun version set detail list with page
     *
      * @param versionSetQueryDto   the vresion set query dto
     *
     * @return  the wrapper.
     */
    @RequestMapping(value = "/queryDetailListWithPage", method = RequestMethod.POST)
    public Wrapper queryDetailListWithPage(@RequestBody VersionSetQueryDto versionSetQueryDto) {

        List<VersionDeviceVo> versionDeviceVoList = dmcCsunVersionSetService.queryDetailListWithPage(versionSetQueryDto);

        return WrapMapper.wrap(Wrapper.SUCCESS_CODE,
                cmnyMessageSource.getMessage("operation.success", null),
                new PageInfo<>(versionDeviceVoList));
    }


    /**
     * query cur task list with page
     *
     * @param versionSetQueryDto
     *
     *
     * @return the wrapper.
     */
    @RequestMapping(value = "/queryCurTaskListWithPage", method = RequestMethod.POST)
    public Wrapper queryCurTaskListWithPage(@RequestBody VersionSetQueryDto versionSetQueryDto) {

        // 根据用户Id查询任务状态码
        List<Integer> states = dmcCsunVersionSetService.queryCurTaskStates(getLoginAuthDto());

        if (PublicUtil.isEmpty(states) || states.size() < 1) {
            return WrapMapper.ok(new PageInfo<>(Lists.newArrayList()));
        }

        // 对states进行去重
        PublicUtil.removeDuplicate(states);

        versionSetQueryDto.setStates(states);

        List<VersionSetVo> versionSetVoList = dmcCsunVersionSetService.queryListWithPage(versionSetQueryDto);

        return WrapMapper.wrap(Wrapper.SUCCESS_CODE, cmnyMessageSource
                .getMessage("operation.success", null), new PageInfo<>(versionSetVoList));
    }


    /**
     * 销售/售后审批升级流程
     *
     * @param versionSetApplyDto    the class version set apply dto
     *
     *
     * @return  the wrapper.
     */
    @RequestMapping(value = "/shouHouApply", method = RequestMethod.POST)
    public Wrapper shouHouApply(@RequestBody VersionSetApplyDto versionSetApplyDto) {

        Integer result = dmcCsunVersionSetService.shouHouApply(versionSetApplyDto, getLoginAuthDto());
        if (result < 1) {
            return WrapMapper.wrap(Wrapper.ERROR_CODE, cmnyMessageSource
                    .getMessage("operation.failed", null));
        }

        return WrapMapper.wrap(Wrapper.SUCCESS_CODE, cmnyMessageSource
                .getMessage("operation.success", null), result);
    }

    /**
     * 查询售后人员审批的情况
     *
     * @param versionSetId  the version set id
     *
     * @return the wrapper.
     */
    @RequestMapping(value = "/queryApplyVarById/{versionSetId}")
    public Wrapper queryApplyVarById(@PathVariable Long versionSetId) {

        Map<String, Object> variable = dmcCsunVersionSetService.queryApplyVarById(versionSetId);

        return WrapMapper.wrap(Wrapper.SUCCESS_CODE, cmnyMessageSource
                .getMessage("operation.success", null), variable);
    }

    /**
     * 总经理审批审计流程
     *
     * @param versionSetApplyDto    the class version set apply dto
     *
     * @return  the wrapper.
     */
    @RequestMapping(value = "/bossApply", method = RequestMethod.POST)
    public Wrapper bossApply(@RequestBody VersionSetApplyDto versionSetApplyDto) {

        Integer result = dmcCsunVersionSetService.bossApply(versionSetApplyDto, getLoginAuthDto());
        if (result < 1) {
            return WrapMapper.wrap(Wrapper.ERROR_CODE, cmnyMessageSource
                    .getMessage("operation.failed", null));
        }

        return WrapMapper.wrap(Wrapper.SUCCESS_CODE, cmnyMessageSource
                .getMessage("operation.success", null), result);
    }

    /**
     * 个人升级设备版本
     *
     * @param versionSetPersonApply the version set person apply
     *
     * @return  the wrapper
     */
    @RequestMapping(value = "/personApply", method = RequestMethod.POST)
    public Wrapper personApply(@RequestBody VersionSetPersonApply versionSetPersonApply) {

        Integer result = dmcCsunVersionSetService.personApply(versionSetPersonApply);
        if (result < 1) {
            return WrapMapper.wrap(Wrapper.ERROR_CODE, cmnyMessageSource
                    .getMessage("operation.failed", null));
        }

        return WrapMapper.wrap(Wrapper.SUCCESS_CODE, cmnyMessageSource
                .getMessage("operation.success", null), result);
    }

    /**
     * 个人查询设备升级版本列表
     *
     * @return the wrapper
     */
    @RequestMapping(value = "/queryUpdateVersionList", method = RequestMethod.POST)
    public Wrapper queryUpdateVersionList() {

        List<AppVersionDeviceVo> appVersionDeviceVoList = dmcCsunVersionSetService.queryUpdateVersionList(getLoginAuthDto());

        /**
         * @author  chisj
         * @desc    修复bug #5347
         * @date    2020-08-07
         */
        if (PublicUtil.isNotEmpty(appVersionDeviceVoList)) {
            // 移除stat不为0的项
            Iterator<AppVersionDeviceVo> iterator = appVersionDeviceVoList.iterator();
            while (iterator.hasNext()) {

                AppVersionDeviceVo a = iterator.next();
                if (!a.getStat().equals(0)) {
                    iterator.remove();
                }
            }
        }

        return WrapMapper.wrap(Wrapper.SUCCESS_CODE, cmnyMessageSource
                .getMessage("operation.success", null),
                appVersionDeviceVoList);
    }

}
